
package com.viweb.storemanager.core;

import com.viweb.storemanager.temp.FloatVerifier;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.text.JTextComponent;


public class TextFieldUtils {

    public static JFormattedTextField createFloatTextField(int fractionDigitsCount) {
        NumberFormat floatNumberFormat = NumberFormat.getNumberInstance(Locale.GERMANY);
        floatNumberFormat.setMinimumFractionDigits(fractionDigitsCount);
        floatNumberFormat.setMaximumFractionDigits(fractionDigitsCount);

        JFormattedTextField formattedTextField = new JFormattedTextField(floatNumberFormat);
        formattedTextField.setInputVerifier(new FloatVerifier(floatNumberFormat));

        return formattedTextField;
    }

    public static JFormattedTextField createFloatTextField(int fractionDigitsCount, boolean negative) {
        NumberFormat floatNumberFormat = NumberFormat.getNumberInstance(Locale.GERMANY);
        floatNumberFormat.setMinimumFractionDigits(fractionDigitsCount);
        floatNumberFormat.setMaximumFractionDigits(fractionDigitsCount);

        JFormattedTextField formattedTextField = new JFormattedTextField(floatNumberFormat);
        formattedTextField.setInputVerifier(new FloatVerifier(floatNumberFormat, negative));

        return formattedTextField;
    }

    public static JFormattedTextField createFloatTextField(int fractionDigitsCount, float max) {
        NumberFormat floatNumberFormat = NumberFormat.getNumberInstance(Locale.GERMANY);
        floatNumberFormat.setMinimumFractionDigits(fractionDigitsCount);
        floatNumberFormat.setMaximumFractionDigits(fractionDigitsCount);

        JFormattedTextField formattedTextField = new JFormattedTextField(floatNumberFormat);
        formattedTextField.setInputVerifier(new FloatVerifier(floatNumberFormat, max));

        return formattedTextField;
    }

    public static JFormattedTextField createFloatTextField(int fractionDigitsCount, float min, float max) {
        NumberFormat floatNumberFormat = NumberFormat.getNumberInstance(Locale.GERMANY);
        floatNumberFormat.setMinimumFractionDigits(fractionDigitsCount);
        floatNumberFormat.setMaximumFractionDigits(fractionDigitsCount);

        JFormattedTextField formattedTextField = new JFormattedTextField(floatNumberFormat);
        formattedTextField.setInputVerifier(new FloatVerifier(floatNumberFormat, min, max));

        return formattedTextField;
    }

    public static JFormattedTextField createFloatTextField(Locale locale, int fractionDigitsCount) {
        NumberFormat floatNumberFormat = NumberFormat.getNumberInstance(locale);
        floatNumberFormat.setMinimumFractionDigits(fractionDigitsCount);
        floatNumberFormat.setMaximumFractionDigits(fractionDigitsCount);

        JFormattedTextField formattedTextField = new JFormattedTextField(floatNumberFormat);
        formattedTextField.setInputVerifier(new FloatVerifier(floatNumberFormat));

        return formattedTextField;
    }

    public static JFormattedTextField createFloatTextField(Locale locale, int fractionDigitsCount, boolean negative) {
        NumberFormat floatNumberFormat = NumberFormat.getNumberInstance(locale);
        floatNumberFormat.setMinimumFractionDigits(fractionDigitsCount);
        floatNumberFormat.setMaximumFractionDigits(fractionDigitsCount);

        JFormattedTextField formattedTextField = new JFormattedTextField(floatNumberFormat);
        formattedTextField.setInputVerifier(new FloatVerifier(floatNumberFormat, negative));

        return formattedTextField;
    }

    public static JFormattedTextField createFloatTextField(Locale locale, int fractionDigitsCount, float max) {
        NumberFormat floatNumberFormat = NumberFormat.getNumberInstance(locale);
        floatNumberFormat.setMinimumFractionDigits(fractionDigitsCount);
        floatNumberFormat.setMaximumFractionDigits(fractionDigitsCount);

        JFormattedTextField formattedTextField = new JFormattedTextField(floatNumberFormat);
        formattedTextField.setInputVerifier(new FloatVerifier(floatNumberFormat, max));

        return formattedTextField;
    }

    public static JFormattedTextField createFloatTextField(Locale locale, int fractionDigitsCount, float min, float max) {
        NumberFormat floatNumberFormat = NumberFormat.getNumberInstance(locale);
        floatNumberFormat.setMinimumFractionDigits(fractionDigitsCount);
        floatNumberFormat.setMaximumFractionDigits(fractionDigitsCount);

        JFormattedTextField formattedTextField = new JFormattedTextField(floatNumberFormat);
        formattedTextField.setInputVerifier(new FloatVerifier(floatNumberFormat, min, max));

        return formattedTextField;
    }

    public static void setFloatValue(float floatValue, JFormattedTextField textField) {
        textField.setValue(new Float(floatValue));
        if (floatValue == 0) {
            textField.setText(null);
        }
    }

    public static float getFloatValue(JFormattedTextField textField) {
        if (textField.getValue() != null) {
            return ((Float) textField.getValue()).floatValue();
        }

        return 0;
    }

    /**
     * Gets the value float type of input string
     * @param value input string
     * @return float return value
     */
    public static float getFloatValue(String value) {
        try {
            NumberFormat floatNumberFormat = NumberFormat.getNumberInstance();
            return floatNumberFormat.parse(value.trim()).floatValue();
        } catch (ParseException ex) {
        }
        return 0;
    }

    public static JFormattedTextField createIntegerTextField() {
        NumberFormat integerNumberFormat = NumberFormat.getNumberInstance(Locale.GERMANY);

        JFormattedTextField formattedTextField = new JFormattedTextField(integerNumberFormat);
        formattedTextField.setInputVerifier(new IntegerVerifier(integerNumberFormat));

        return formattedTextField;
    }

    public static JFormattedTextField createIntegerTextField(boolean negative) {
        NumberFormat integerNumberFormat = NumberFormat.getNumberInstance(Locale.GERMANY);

        JFormattedTextField formattedTextField = new JFormattedTextField(integerNumberFormat);
        formattedTextField.setInputVerifier(new IntegerVerifier(integerNumberFormat, negative));

        return formattedTextField;
    }

    public static JFormattedTextField createIntegerTextField(int max) {
        NumberFormat integerNumberFormat = NumberFormat.getNumberInstance(Locale.GERMANY);

        JFormattedTextField formattedTextField = new JFormattedTextField(integerNumberFormat);
        formattedTextField.setInputVerifier(new IntegerVerifier(integerNumberFormat, max));

        return formattedTextField;
    }

    public static JFormattedTextField createIntegerTextField(int min, int max) {
        NumberFormat integerNumberFormat = NumberFormat.getNumberInstance(Locale.GERMANY);

        JFormattedTextField formattedTextField = new JFormattedTextField(integerNumberFormat);
        formattedTextField.setInputVerifier(new IntegerVerifier(integerNumberFormat, min, max));

        return formattedTextField;
    }

    public static JFormattedTextField createIntegerTextField(Locale locale) {
        NumberFormat integerNumberFormat = NumberFormat.getNumberInstance(locale);

        JFormattedTextField formattedTextField = new JFormattedTextField(integerNumberFormat);
        formattedTextField.setInputVerifier(new IntegerVerifier(integerNumberFormat));

        return formattedTextField;
    }

    public static JFormattedTextField createIntegerTextField(Locale locale, boolean negative) {
        NumberFormat integerNumberFormat = NumberFormat.getNumberInstance(locale);

        JFormattedTextField formattedTextField = new JFormattedTextField(integerNumberFormat);
        formattedTextField.setInputVerifier(new IntegerVerifier(integerNumberFormat, negative));

        return formattedTextField;
    }

    public static JFormattedTextField createIntegerTextField(Locale locale, int max) {
        NumberFormat integerNumberFormat = NumberFormat.getNumberInstance(locale);

        JFormattedTextField formattedTextField = new JFormattedTextField(integerNumberFormat);
        formattedTextField.setInputVerifier(new IntegerVerifier(integerNumberFormat, max));

        return formattedTextField;
    }

    public static JFormattedTextField createIntegerTextField(Locale locale, int min, int max) {
        NumberFormat integerNumberFormat = NumberFormat.getNumberInstance(locale);

        JFormattedTextField formattedTextField = new JFormattedTextField(integerNumberFormat);
        formattedTextField.setInputVerifier(new IntegerVerifier(integerNumberFormat, min, max));

        return formattedTextField;
    }

    public static void setIntegerValue(int integerValue, JFormattedTextField textField) {
        textField.setValue(new Integer(integerValue));
        if (integerValue == 0) {
            textField.setText(null);
        }
    }

    public static int getIntegerValue(JFormattedTextField textField) {
        if (textField.getValue() != null) {
            return ((Integer) textField.getValue()).intValue();
        }

        return 0;
    }

    /**
     * Gets the value int type of input string
     * @param value input string
     * @return int return value
     */
    public static int getIntValue(String value) {
        try {
            return (value == null ? 0 : (value.length() == 0 ? 0 : Integer.parseInt(value.trim())));
        } catch (Exception ex) {
            return 0;
        }
    }

    public static void setNextFocusComponent(JTextComponent textComponent, JComponent nextFocusComponent) {
        final JComponent component = nextFocusComponent;
        textComponent.addKeyListener(new java.awt.event.KeyAdapter() {

            @Override
            public void keyPressed(java.awt.event.KeyEvent evt) {
                if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
                    component.requestFocus();
                }
            }
        });
    }

    public static void setNextFocusComponent(JTextComponent textComponent, JComponent nextFocusComponent, int key) {
        if (key == java.awt.event.KeyEvent.VK_ENTER) {
            setNextFocusComponent(textComponent, nextFocusComponent);
            return;
        }

        final int keyValue = key;
        final JComponent component = nextFocusComponent;
        textComponent.addKeyListener(new java.awt.event.KeyAdapter() {

            @Override
            public void keyPressed(java.awt.event.KeyEvent evt) {
                if (evt.getKeyCode() == keyValue) {
                    component.requestFocus();
                }
            }
        });
    }
}
